<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 
/**
 * STBlog
 *
 * 基于Codeigniter的单用户多权限开源博客系统
 * 
 *
 * @package		STBLOG
 * @author		Saturn
 * @copyright	Copyright (c) 2009 - 2010, cnsaturn.com.
 * @license		GNU General Public License 2.0
 * @link		http://code.google.com/p/stblog/
 * @since		Version 0.1
 * @filesource
 */
 
// ------------------------------------------------------------------------

/**
 * STBLOG Users Class
 *
 * 本类用于Users管理逻辑
 *
 * @package		STBLOG
 * @subpackage	Controller
 * @category	Admin Controller
 * @author		Saturn <huyanggang@gmail.com>
 * @link 		http://code.google.com/p/stblog/
 */
class Links extends ST_Auth_Controller {
	
	/**
     * 传递到对应视图的数据
     *
     * @access private
     * @var array
     */
	private $_data = array();
	
	/**
     * 当前用户ID
     *
     * @access private
     * @var integer
     */	
	private $_uid = 0;

	/**
     * 解析函数
     *
     * @access public
     * @return void
     */
	public function __construct()
	{
		parent::__construct();
		
		/** privilege confirm */
		$this->auth->exceed('administrator');
		
		/** common data */
		$this->_data['parentPage'] = 'manage-posts';
		$this->_data['currentPage'] = 'manage-link';
		$this->_data['page_title'] = '友情链接';
	}

	 /**
     * 配置表单验证规则
     * 
     * @access private
     * @return void
     */
	private function _load_validation_rules()
	{
		$this->form_validation->set_rules('title', '标题', 'required|trim|alpha_numeric|callback__name_check|strip_tags');
		$this->form_validation->set_rules('url', '个人主页', 'trim|prep_url');
		$this->form_validation->set_rules('norder', '排序', 'trim');
		$this->form_validation->set_rules('type', '类型', 'trim');
	}
	


	 /**
     * 添加一个用户
     * 
     * @access 	private
     * @return 	void
     */
	private function _add_link()
	{
		
		

		$this->_data['page_title'] = '增加用户';			
		$this->_load_validation_rules();
		
		if (empty($_POST))
		{
			$this->load->view('admin/add_link',$this->_data);
		}
		else
		{

			$this->links_mdl->add_link(
				array(
					'title' 	=>	$this->input->post('title',TRUE),
					'type'		=>	$this->input->post('type',TRUE),
					'norder'	=>	$this->input->post('norder',TRUE),
					'url'		=>	$this->input->post('url',TRUE)
				)
			);
			
			$this->session->set_flashdata('success', '成功添加一个链接');
			go_back();
		}
	}

	 /**
     * 编辑一个用户
     * 
     * @access 	private
     * @param 	$uid
     * @return 	void
     */
	private function _edit_user($id)
	{
		$this->_uid = $id;
		$user = $this->links_mdl->get_user_by_id($id);

		if(!$user)
		{
			show_error('用户不存在或已经被删除');
			exit();
		}
		
		$this->_data['id'] = $user['id'];
		$this->_data['title'] = $user['title'];
		$this->_data['type'] = $user['type'];
		$this->_data['norder'] = $user['norder'];
		$this->_data['url'] = $user['url'];
		$this->_data['page_title'] = '编辑用户: '.$user['title'];
	
		$this->_load_validation_rules();
		
		if (empty($_POST))
		{	
			$this->load->view('admin/add_link',$this->_data);
		}
		else
		{
			$this->links_mdl->update_link(
				$id,
				array(
					'title' 	=>	$this->input->post('title',TRUE),
					'type'		=>	$this->input->post('type',TRUE),
					'url'		=>	$this->input->post('url',TRUE),
					'norder'		=>	$this->input->post('norder',TRUE)
				),
				FALSE
			);
			
			$this->session->set_flashdata('success', '成功修改链接 ');
			go_back();
		}
	}
	
	 /**
     * 默认执行函数
     * 
     * @access public
     * @return void
     */
	public function index()
	{
		redirect('admin/links/manage');
	}

	 /**
     * 用户管理列表
     * 
     * @access public
     * @return void
     */
	public function manage()
	{
	
		$users = $this->links_mdl->get_users();



		$this->_data['links'] = $users;
		
		$this->load->view('admin/manage_links',  $this->_data);	
		
		
		
	
	}

	 /**
     * 用户操作分发器
     * 
     * @access public
     * @return void
     */
	public function user()
	{
		if (FALSE === $this->uri->segment(4))
		{
			$this->_add_link();
		}
		else
		{
			$uid = $this->input->xss_clean($this->uri->segment(4));
			is_numeric($uid)?$this->_edit_user($uid):show_error('禁止访问：危险操作');
		}
	
	}

	 /**
     * 批量删除用户
     * 
     * @access public
     * @return void
     */
	public function remove()
	{

		$users = $this->input->post('id',TRUE);
		$deleted = 0;
		
		if($users && is_array($users))
		{
			foreach($users as $user)
			{			
				$this->links_mdl->remove_user($user);
				$deleted++;
			}
		
		}
		
		$msg = ($deleted > 0)?'链接已经删除':'没有链接被删除';
        $notify = ($deleted > 0)?'success':'error';
        
        $this->session->set_flashdata($notify, $msg);
		go_back();
	
	}
	
		
}

/* End of file Users.php */
/* Location: ./application/controllers/admin/Users.php */